//
//  EmotionListView.m
//  Weibo
//
//  Created by Anasue on 9/25/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#define EmotionCountPerPage (EmotionMaxColsPerPage *EmotionMaxRowsPerPage - 1)


#import "EmotionListView.h"
#import "EmotionGridView.h"

@interface EmotionListView ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIPageControl *pageControl;

@end

@implementation EmotionListView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //创建scrollView
        [self setupScrollView];
        //创建滚动条
        [self setupPageControl];
    }
    return self;
}

#pragma  mark - 创建子控件
/**
 *  创建scrollView
 */
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self addSubview:scrollView];
    self.scrollView = scrollView;
}

/**
 *  创建滚动条
 */
- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    [pageControl setValue:[UIImage imageWithName:@"compose_keyboard_dot_selected"] forKeyPath:@"_currentPageImage"];
    [pageControl setValue:[UIImage imageWithName:@"compose_keyboard_dot_normal"] forKeyPath:@"_pageImage"];
    self.pageControl = pageControl;
    [self addSubview:pageControl];
}

- (void)setEmotions:(NSArray *)emotions
{
    _emotions = emotions;
    self.pageControl.numberOfPages = (emotions.count + EmotionCountPerPage - 1)/EmotionCountPerPage;
    self.pageControl.currentPage = 0;
    self.pageControl.hidden = self.pageControl.numberOfPages <=1;
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i = 0; i <self.pageControl.numberOfPages; i ++) {
        EmotionGridView *gridView = [[EmotionGridView alloc]init];
        NSInteger loc = i * EmotionCountPerPage;
        NSInteger len = EmotionCountPerPage;
        if (loc + len > emotions.count) {
            len = emotions.count - loc;
        }
        NSRange gridViewRange = NSMakeRange(loc, len);
        NSArray *gridViews = [emotions subarrayWithRange:gridViewRange];
        gridView.emotions = gridViews;
        [self.scrollView addSubview:gridView];
    }
    [self setNeedsLayout];
    self.scrollView.contentOffset = CGPointZero;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.pageControl.height = 30;
    self.pageControl.width = self.width;
    self.pageControl.y = self.height - self.pageControl.height;
    self.scrollView.width = self.width;
    self.scrollView.height = self.pageControl.y;
    
    NSInteger count = self.pageControl.numberOfPages;
    CGFloat gridViewW = self.scrollView.width;
    CGFloat gridViewH = self.scrollView.height;
    self.scrollView.contentSize = CGSizeMake(count * gridViewW, 0);
    for (NSInteger i = 0; i <count; i ++) {
        EmotionGridView *gridView = self.scrollView.subviews[i];
        gridView.width = gridViewW;
        gridView.height = gridViewH;
        gridView.x = i * gridViewW;
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.width + 0.5);
}


@end
